package com.irdstudio.tdp.executor.core.plugin.gencode.dao;

import com.irdstudio.tdp.executor.core.plugin.gencode.vo.AppModelInfo;
import com.irdstudio.tdp.executor.core.plugin.gencode.vo.OsrvNodeInfo;
import com.irdstudio.tdp.executor.core.plugin.util.POJOUtils;
import com.irdstudio.tdp.executor.core.plugin.util.ProxySQL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class AppModelInfoDao {
    private static final Logger logger = LoggerFactory.getLogger(AppModelInfoDao.class);
    /* 连接对象 */
    Connection conn = null;

    public AppModelInfoDao(Connection conn){
        this.conn = conn;
    }

    public List<AppModelInfo> queryAppModelInfoList(AppModelInfo appModelInfo) throws Exception {
        List param = new ArrayList();
        String sql = new ProxySQL() {{
            SELECT("*");
            FROM("app_model_info");
            WHERE("app_id = ?");
            param.add(appModelInfo.getAppId());
            WHERE("app_model_catalog = ? ");
            param.add(appModelInfo.getAppModelCatalog());

        }}.toString();
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<AppModelInfo> list = new ArrayList<>();
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < param.size(); i++) {
                ps.setObject(i + 1, param.get(i));
            }
            rs = ps.executeQuery();
            while (rs.next()) {
                AppModelInfo bean = POJOUtils.generatePOJO(rs, AppModelInfo.class);
                list.add(bean);
            }

        } catch (Exception e) {
            throw new RuntimeException("query queryOsrvNodeInfoList is wrong", e);
        }
        return list;
    }

    public AppModelInfo queryByPk(AppModelInfo appModelInfo) throws Exception {
        List param = new ArrayList();
        String sql = new ProxySQL() {{
            SELECT("*");
            FROM("app_model_info");
            WHERE("app_model_id = ?");
            param.add(appModelInfo.getAppModelId());

        }}.toString();
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<AppModelInfo> list = new ArrayList<>();
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < param.size(); i++) {
                ps.setObject(i + 1, param.get(i));
            }
            rs = ps.executeQuery();
            if (rs.next()) {
                AppModelInfo bean = POJOUtils.generatePOJO(rs, AppModelInfo.class);
                list.add(bean);
            }

        } catch (Exception e) {
            throw new RuntimeException("query queryByPk is wrong", e);
        }
        return list.isEmpty() ? null : list.get(0);
    }
}
